//
//  MYTabbar.swift
//  sina
//
//  Created by liululu on 16/4/6.
//  Copyright © 2016年 liululu. All rights reserved.
//

import UIKit

class MYTabbar: UITabBar {

    //重写父类的init
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(plusBtn)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let width = self.frame.width/5
        let height = self.bounds.height
        
        let rect = CGRect(x: 0, y: 0, width: width, height: height)
        var index : CGFloat = 0
        
        for subview in subviews{
            print(subview)
            
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
                subview.frame = CGRectOffset(rect, index * width, 0)
                
                index += (index == 1) ? 2 : 1
            }
        }
        
        plusBtn.frame = CGRectOffset(rect, 2 * width, 0)
    }
    
    
    //懒加载plusbtn
    private lazy var plusBtn: UIButton = {
        let btn = UIButton()

        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
       return btn
    }()
}
